﻿using System;
using System.Windows.Forms;
using System.Data;

namespace template_dll
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }

        public DataTable dt;
        public string button1Text = "Применить ко всем строкам";
        public string button2Text = "Сохранить для расчета";
        public string formText = "";
        public string gbText = "";
        public string tbText = "";
        public bool active = true;

        private void Form7_Load(object sender, EventArgs e)
        {
            textBox1.Text = tbText;
            dataGridView1.DataSource = dt.DefaultView;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            
            this.Text = formText;
            groupBox1.Text = gbText;
            button1.Text = button1Text;
            button2.Text = button2Text;
            if (active)
            {
                textBox1.Enabled = true;
                button1.Enabled = true;
                dataGridView1.Columns[2].ReadOnly = false;
            }
            else
            {
                textBox1.Enabled = false;
                button1.Enabled = false;
                dataGridView1.Columns[2].ReadOnly = true;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!(Char.IsDigit(e.KeyChar)))
            //{
            //    if (e.KeyChar != (char)Keys.Back)
            //    {
            //        e.Handled = true;
            //    }
            //}
            if (!(Char.IsDigit(e.KeyChar)) && !((e.KeyChar == ',') && (textBox1.Text.IndexOf(",") == -1) && (textBox1.Text.Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back) e.Handled = true;
            }
        }

        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!(Char.IsDigit(e.KeyChar)))
            //{
            //    if (e.KeyChar != (char)Keys.Back)
            //    {
            //        e.Handled = true;
            //    }
            //}
            if (!(Char.IsDigit(e.KeyChar)) && !((e.KeyChar == ',') && (dataGridView1.CurrentCell.Value.ToString().IndexOf(",") == -1) && (dataGridView1.CurrentCell.Value.ToString().Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back) e.Handled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Установить значение для всех строк?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DataRow row in dt.Rows)
                {
                    row[2] = textBox1.Text;
                }
                dataGridView1.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Применить данные настройки?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
